package com.maoyu.cloud.role.controller;

import com.maoyu.cloud.mybatis.entity.PtRoleEntity;
import com.maoyu.cloud.mybatis.entity.ZtreeEntity;
import com.maoyu.cloud.mybatis.flexigrid.PadingRstType;
import com.maoyu.cloud.mybatis.flexigrid.PagingBean;
import com.maoyu.cloud.role.entity.SearchRoleEntity;
import com.maoyu.cloud.role.service.RoleService;
import com.maoyu.cloud.spring.controller.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController extends BaseController {
    @Resource
    private RoleService roleService;
    private final Logger logger = LogManager.getLogger(RoleController.class.getName());


    @Secured("ROLE_cldk_data_role")
    @RequestMapping("/rolePage")
    public String rolePage(){
        return "role/rolePage";
    }

    @RequestMapping("/queryRoleListByPage")
    @ResponseBody
    public PadingRstType<PtRoleEntity> queryRoleListByPage(SearchRoleEntity entity, PagingBean paging){
        logger.info(entity);
        logger.info(paging);
        return roleService.queryRoleListByPage(entity,paging);
    }

    @RequestMapping("/queryZtreeByRoleId")
    @ResponseBody
    public List<ZtreeEntity> queryZtreeByRoleId(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid:"+roleUuid);
        List<ZtreeEntity> list = roleService.queryZtreeByRoleId(roleUuid);
        return list;
    }
    @RequestMapping("/saveMenuRefRole")
    @ResponseBody
    public String saveMenuRefRole(@RequestParam("menuIdArray")String menuIdArray,@RequestParam("roleUuidArray")String roleUuidArray){
        logger.info("menuIdArray:" +menuIdArray+ "roleUuidArray:" +roleUuidArray);
        roleService.saveMenuRefRole(menuIdArray,roleUuidArray);

        return successJson("role.menu.allocate.success");

    }

    @RequestMapping("/queryMenuZtreeAll")
    @ResponseBody
    public List<ZtreeEntity> queryMenuZtreeAll(){
        List<ZtreeEntity> list = roleService.queryMenuZtreeAll();
        return list;
    }

    @RequestMapping("/addroleInfoById")
    @ResponseBody
    public String addroleInfoById(PtRoleEntity entity) {
        logger.info(entity);
        roleService.addroleInfoById(entity);
        return successJson("role.add.info.success");
    }

    /**
     * 删除角色
     * @param roleUuid
     * @return
     */
    @RequestMapping("/deleteRoleById")
    @ResponseBody
    public String deleteRoleById(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid:"+roleUuid);
        roleService.deleteRoleById(roleUuid);
        return successJson("role.delete.info.success");
    }

    /**
     * 修改角色
     * @param entity
     * @return
     */
    @RequestMapping("/updateRoleById")
    @ResponseBody
    public String updateRoleById(PtRoleEntity entity){
        logger.info(entity);
        roleService.updateRoleById(entity);
        return successJson("role.update.info.success");
    }
    /**
     * 查询角色
     * @param roleUuid
     * @return
     */
    @RequestMapping("/queryRoleInfoById")
    @ResponseBody
    public PtRoleEntity queryRoleById(@RequestParam("roleUuid") String roleUuid){
        logger.info("roleUuid:"+roleUuid);
        return roleService.queryRoleById(roleUuid);
    }

}